/*-*-C-*-
 * objedit include file
 */

#ifndef __objedit_h
#define __objedit_h


/* allows forward references to ObjectPtr's */
typedef struct _objectrec ObjectRec, *ObjectPtr;

/***************************************************************************/

/*
 * The properties and structure of each miscellaneous object class are
 *  defined by a structure of type ObjectDefRec.
 *
 * This structure is interpreted at run-time to:
 *   - load an object record from a template
 *   - save an object record to a template
 *   - delete an object record
 *   - fill in a dialogue box describing the object's properties from an
 *      object record
 *   - update an object's properties dialogue box according to user
 *      interactions
 *   - update an object record from the corresponding properties dialogue box
 */


/*
 * The RefDefRec structure provides information about a field which requires
 *  relocation.
 */

#define  REF_STR       (0)   /* a StringReference field */
#define  REF_MSG       (1)   /* a MsgReference field */
#define  REF_SPRITE    (2)   /* a SpriteAreaReference field */
#define  REF_OFFSET    (3)   /* an ObjectOffset reference field */
#define  REF_END       (4)   /* this terminates an array of reference defs */

typedef struct
{
    int type;
    int offset;
    Bool emptyisnull;
} RefDefRec, *RefDefPtr;


/*
 * This structure identifies those fields inside an object template which
 *  need to be relocated when the object template is loaded or saved.
 *
 * Each of the pointer fields addresses a sequence of definitions which is
 *  terminated by a REF_END definition.
 */

typedef struct
{
    int size;          /* object template size in bytes */
    RefDefPtr refs;    /* describes each relocation; must not be NULL */
} BodyDefRec, *BodyDefPtr;


/*
 * The following structures define the relationships between fields in an
 *  object template and fields in its properties dialogue box.
 *
 * Each (group of) fields is defined by a FieldDefRec, which includes all
 *  the information needed at run-time to transfer data in either direction
 *  between an object template and the corresponding dbox.
 *
 * For example, a FieldDefRec that points to an IntegerFldRec describes a
 *  simple integer field inside a template:
 *     - 'valicon' gives the number of the icon in the dbox which is to
 *        contain the value
 *     - 'offset' is the offset of the field which contains the value
 *        (relative to the start of the object template)
 *     - 'displayashex' is TRUE iff the value is to be displayed in hex
 *        inside the dialogue box.
 *
 * Each type of field is identified by one of the following #defines.
 */


#define  FLD_INTEGER         (0)  /* simple integer writable */
#define  FLD_MAND_ASS_STR    (1)  /* mandatory assignable string field:
                                      value, length and adjuster icons */
#define  FLD_OPT_ASS_STR     (2)  /* optional assignable string field:
                                      as above, plus option icon */
#define  FLD_TITLE           (3)  /* like FLD_OPT_ASS_STR, except with two
                                      radio icons in place of opticon */
#define  FLD_MAND_CONST_STR  (4)  /* simple string field! */
#define  FLD_OPT_CONST_STR   (5)  /* optional simple string field */
#define  FLD_FLAG            (6)  /* option icon linked to flag bit */
#define  FLD_OPT_EVENT       (7)  /* event can be none, default or other */
#define  FLD_FILETYPE        (8)  /* as, eg, "aaaaaaaa (&ddd)" */
#define  FLD_END             (9)  /* this terminates an array of fielddefs */


typedef struct
{
    int valicon;
    int offset;
    Bool displayashex;
} IntegerFldRec, *IntegerFldPtr;


typedef struct
{
    int valicon;
    int lenicon;
    int upicon;
    int downicon;
    int valoffset;
    int lenoffset;
} MandAssStrFldRec, *MandAssStrFldPtr;


typedef struct
{
    int opticon;
    int valicon;
    int lenicon;
    int upicon;
    int downicon;
    int valoffset;
    int lenoffset;
} OptAssStrFldRec, *OptAssStrFldPtr;


typedef struct
{
    int dflticon;
    int othericon;
    int valicon;
    int lenicon;
    int upicon;
    int downicon;
    int valoffset;
    int lenoffset;
} TitleFldRec, *TitleFldPtr;


typedef struct
{
    int icon;
    int offset;
} MandConstStrFldRec, *MandConstStrFldPtr;


typedef struct
{
    int opticon;
    int valicon;
    int offset;
} OptConstStrFldRec, *OptConstStrFldPtr;


typedef struct
{
    int opticon;
    unsigned mask;
    Bool flagmeanson;
} FlagFldRec, *FlagFldPtr;


typedef struct
{
    int dflticon;
    int othericon;
    int noneicon;
    int valicon;
    int offset;
    int mask;
} OptEventFldRec, *OptEventFldPtr;


typedef struct
{
    int valicon;
    int offset;
} FiletypeFldRec, *FiletypeFldPtr;


typedef union
{
    IntegerFldRec integer;
    MandAssStrFldRec mandassstr;
    OptAssStrFldRec optassstr;
    TitleFldRec title;
    MandConstStrFldRec mandconststr;
    OptConstStrFldRec optconststr;
    FlagFldRec flag;
    OptEventFldRec optevent;
    FiletypeFldRec filetype;
} FldDefRec, *FldDefPtr;


typedef struct
{
    int type;
    FldDefPtr def;
} FieldDefRec, *FieldDefPtr;


/*
 * The following structures define actions which must be taken when the user
 *  clicks on certain icons in the dialogue box.
 *
 * For example, if the user clicks on the option icon for a Help message,
 *  then the Help text field must have its 'faded' status toggled.
 *
 * Possible actions are #defined in the following list.
 */

#define  ACT_FADE        (0)  /* this is a radio icon: fade a related icon */
#define  ACT_UNFADE      (1)  /* as above, but unfade a related icon */
#define  ACT_TOGGLEFADE  (2)  /* this is an option icon: toggle a related
                                  icon's 'faded' status */
#define  ACT_RADIO       (3)  /* this is a radio button: adjust-click must
                                  not switch it off */
#define  ACT_ADJUST      (4)  /* this is an adjuster arrow: a related
                                  writable must be updated */
#define  ACT_TITLE       (5)  /* click on title radio or adjuster button */
#define  ACT_SPECIAL     (6)  /* escape route - calls a given function when
                                  any mouse button is clicked on the icon */
#define  ACT_END         (7)  /* this terminates an array of actions */


typedef struct
{
    int valicon;
} FadeClickRec, *FadeClickPtr;


typedef struct
{
    int valicon;
} UnFadeClickRec, *UnFadeClickPtr;


typedef struct
{
    int valicon;
} ToggleFadeClickRec, *ToggleFadeClickPtr;


typedef struct
{
    int valicon;    /* set to -1 if not relevant */
    int lenicon;
    Bool increase;
    int increment;
    int shiftincrement;
} AdjustClickRec, *AdjustClickPtr;


typedef struct
{
    int dflticon;
    int othericon;
    int valicon;
    int lenicon;
    int upicon;
    int downicon;
} TitleClickRec, *TitleClickPtr;


typedef struct
{
    error * (*f) (ObjectPtr object, int icon, MouseClickPtr mouse);
} SpecialClickRec, *SpecialClickPtr;


typedef union
{
    FadeClickRec fade;
    UnFadeClickRec unfade;
    ToggleFadeClickRec togglefade;
    AdjustClickRec adjust;
    TitleClickRec title;
    SpecialClickRec special;
} ClickParamsRec, *ClickParamsPtr;


typedef struct
{
    int icon;
    int action;
    ClickParamsPtr params;
} ClickDefRec, *ClickDefPtr;


/*
 * The following structures define actions which must be taken when the user
 *  drops an object or gadget icon on certain icons in the dialogue box.
 *
 * For example, if the user drags a window template icon from a document
 *  window and drops it onto the "alternative window" icon in a DCS
 *  properties dialogue box, then the name of the window template must be
 *  inserted into the icon.
 *
 * Possible actions are #defined in the following list.
 */

#define  DROP_SET         (1)   /* single writable */
#define  DROP_SETOPT      (2)   /* opticon + writable */
#define  DROP_SETOPT2     (3)   /* opticon + writable + dependent icon */
#define  DROP_SETOPT3     (4)   /* opticon + writable + two dependents */


typedef struct
{
    int valicon;
} SetDropRec, *SetDropPtr;


typedef struct
{
    int valicon;
    int opticon;
} SetOptDropRec, *SetOptDropPtr;


typedef struct
{
    int valicon;
    int opticon;
    int othericon;
} SetOpt2DropRec, *SetOpt2DropPtr;


typedef struct
{
    int valicon;
    int opticon;
    int othericon1;
    int othericon2;
} SetOpt3DropRec, *SetOpt3DropPtr;


typedef union
{
    SetDropRec set;
    SetOptDropRec setopt;
    SetOpt2DropRec setopt2;
    SetOpt3DropRec setopt3;
} DropParamsRec, *DropParamsPtr;


 /* The kinds of drop that can occur are defined in the following list */

#define  OBJECT_DROP  (2)

typedef struct
{
    int type;     /* must be an object drop */
    int class;    /* -1 => any class accepted */
    int action;
    DropParamsPtr params;
} DropDetailsRec, *DropDetailsPtr;


typedef struct
{
    int icon;                 /* -2 => end-of-list,  -1 => any icon */
    DropDetailsPtr details;
} DropDefRec, *DropDefPtr;


/*
 * The following structure is a sub-structure for ObjectDefRec's.
 * It gives icon handles for those icons common to all object template
 *  property dialogue boxes.
 */
typedef struct
{
    int firstwritable;       /* icon to take caret when dbox opened */
    int ok;                  /* OK action button */
    int cancel;              /* Cancel action button */
} CommonIconsRec, *CommonIconsPtr;


/*
 * This structure defines the properties and structure of an object template
 *  of a particular type.
 *
 * The 'fields' array is terminated by a field of type FLD_END, and the
 *  'clicks' array by an action of type ACT_END.
 *
 * The 'drops' array is terminated by a record with icon number -2. 
 */

typedef struct
{
    int class;              /* the object's class */
    int version;            /* object class's version */
    char *templatename;     /* name of dbox template in Templates file */
    WindowPtr proto;        /* prototype window for properties dbox */
    int protosize;          /* size of prototype window in bytes */
    BodyDefRec body;        /* defines structure of object template */
    FieldDefPtr fields;     /* defines the fields of the dbox */
    ClickDefPtr clicks;     /* defines the effect of mouse clicks in dbox */
    DropDefPtr drops;       /* defines the effect of DATASAVEs to the dbox */
    CommonIconsRec icons;   /* gives handles of standard dbox icons */
        /* if special processing is required ... */
    error * (*specialstartup) (void);           /* ... at program start-up */
    error * (*specialinit) (ObjectPtr object);  /* ... to initialise dbox */
    error * (*specialapply) (ObjectPtr object); /* ... to apply dbox */
} ObjectDefRec, *ObjectDefPtr;


/***************************************************************************/


/* ----------------------------------------------------------------------- */

/*
 * Each object template which is being edited (ie which has been sent to us
 * from the shell) is represented by an ObjectRec.
 */


/* this is the full definition of ObjectRec and *ObjectPtr */
struct _objectrec
{
    struct _objectrec *next;

    ObjectDefPtr def;           /* addresses the object's class definition */
    WindowPtr dbox;             /* addresses the object's properties dbox */

    char *bodycopy;             /* for "unknown" objects only */

    char name[MAX_OBJECT_NAME]; /* as passed from the shell */
    Opaque documentID;          /* together these provide a unique id */
    Opaque objectID;            /* for the object - quoted by the shell */

    Bool pendingclose;          /* close when transfer to shell complete */
    char *objectdata;           /* used when reassembling template */

    ObjectTemplateRec body;     /* the object record itself; must be last,
                                   since its length depends on the object's
                                   class */
};   /* ObjectRec, *ObjectPtr */


/* ----------------------------------------------------------------------- */


extern char *copystring (char *src);
extern error * clonestring (char **s);
extern Bool equalstrings (char *s, char *t);
extern error * objedit_load (ObjectPtr object,
    ResourceFileObjectTemplateHeaderPtr obj,
    MessageResEdObjectLoadPtr load,
    ObjectDefPtr def);
extern error * objedit_rename_object (ObjectPtr object, char *name);
extern error * objedit_close_object (ObjectPtr object, Bool notifyshell);
extern int objedit_object_size (ObjectPtr object, int *bodysize,
    int *stringsize, int *msgsize, int *numrelocs);
extern error * objedit_save_object_to_memory (ObjectPtr object, char *buffer,
    int bodysize, int stringsize, int msgsize, int numrelocs);


#endif
